package com.cyufeng.blog.service;

import com.cyufeng.blog.dto.ImagesDto;
import com.cyufeng.blog.entity.Images;
import com.cyufeng.blog.repository.ImagesRepository;
import com.cyufeng.blog.service.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * 轮播图
 * @author Cyufeng
 * Data:2018.12.30
 */
@Service
public class ImagesService {
    @Autowired
    ImagesRepository indexRepository;

    ImagesMapper imagesMapper = new ImagesMapper();
//得到所有的轮播图片
    public List<ImagesDto> getImages() {
        List<Images> imagesList = indexRepository.getImages();
        List<ImagesDto> imagesDtoList = imagesMapper.entityTodto(imagesList);
        return imagesDtoList;
    }

    /**
     * 得到所有图片--用于后台操作
     * @return
     */
    public List<ImagesDto> getAllImages() {
        List<Images> imagesList = indexRepository.getAllImages();
        List<ImagesDto> imagesDtoList = imagesMapper.entityTodto(imagesList);
        return imagesDtoList;
    }

    //    修改是否启用
    @Transactional
    public String setIsEnbale(Boolean isEnable, int id) {
        int tempIsEnable = 0;
        try {
            if (isEnable == true)
                tempIsEnable = 1;
            indexRepository.setIsEnable(tempIsEnable, id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    删除图片
    @Transactional
    public String deleteImage(int id) {
        try {
            indexRepository.deleteImage(id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    上传图片-仅路径
    @Transactional
    public String addImage(String url,String fileName) {
        try {

            Images images = new Images();
            images.setSrc(url);
            images.setName(fileName);
            images.setIsEnable(0);
            images.setDr(0);
            images.setMadeTime(new Date());
            indexRepository.save(images);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }
}
